package com.itheima.reggie.controller;

import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Result;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //获取类型集合page
    @RequestMapping("/category/page")
    @ResponseBody
    public Result page(Integer page , Integer pageSize , HttpSession httpSession ){
        return categoryService.page( page , pageSize );
    }

    //添加分类
    @PostMapping("/category")
    @ResponseBody
    public Result add(@RequestBody Category category , HttpSession httpSession){
        Long employee = (Long) httpSession.getAttribute("employee");
        category.setCreateUser(employee);
        category.setUpdateUser(employee);
        categoryService.addCategory(category);
        return Result.success(null);
    }

    //编辑分类
    @PutMapping("/category")
    @ResponseBody
    public Result edit(@RequestBody Category category , HttpSession httpSession ){
        Long employee = (Long) httpSession.getAttribute("employee");
        category.setUpdateUser(employee);
        categoryService.editCategory(category);
        return Result.success(null);
    }

    @DeleteMapping("/category")
    @ResponseBody
    public Result delete( Long id ){
        categoryService.deleteCategory(id);
        return Result.success(null);
    }

    //获取类型集合
    @RequestMapping("/category/list")
    @ResponseBody
    public Result list( Integer type ){

        return  categoryService.findAllByType(type);

    }

}
